﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting.Native;
using BLL;

namespace QuanLyKhachSan.FormTimKiem
{
    public partial class FormTimLoaiPhong : XtraForm
    {
        public FormTimLoaiPhong()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            String content = txtNoiDungTim.Text;
            IEnumerable<Exception> errors = KiemTraGiaTriHopLe(content);
            var exceptions = errors as Exception[] ?? errors.ToArray();
            if (!exceptions.Any())
            {
                Func<LoaiPhong, bool> newFilter;
                newFilter = lp => lp.TenLoaiPhong.Contains(content);
                var list = new LoaiPhongManager().LayDanhSach().Result.Where(newFilter);
                var loaiphongs = list as LoaiPhong[] ?? list.ToArray();
                if (!loaiphongs.Any())
                {
                    XtraMessageBox.Show("Không tìm thấy");
                }
                else
                {
                    Result = loaiphongs;
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                lbInfo.Text = exceptions.ToList()[0].ToString();
            }
        }

        private IEnumerable<Exception> KiemTraGiaTriHopLe(String value)
        {
            var exceptions = new List<Exception>();
            if (value.IsEmpty())
                exceptions.Add(new Exception("Vui lòng nhập loại phòng cần tìm"));
            return exceptions;
        }

        public Func<LoaiPhong, bool> Filter { get; set; }

        public IEnumerable<LoaiPhong> Result { get; set; }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
